package com.cnsoft.player.h5.ques;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.cnsoft.framework.beans.common.RESTResultBean;
import org.cnsoft.framework.cache.session.SessionHelper;
import org.cnsoft.framework.constants.ECCodeMessageConstants;
import org.cnsoft.framework.db.page.PageModel;
import org.cnsoft.framework.support.MyBusinessSupport;
import org.cnsoft.framework.utils.DateHelper;
import org.cnsoft.framework.utils.EmptyHelper;
import org.springframework.stereotype.Component;

import com.cnsoft.persistent.communal2.R713010Ques.R713010QuesDBO;
import com.cnsoft.persistent.communal2.R713010Ques.R713010QuesDao;
import com.cnsoft.persistent.communal2.R713020QuesSub.R713020QuesSubDBO;
import com.cnsoft.persistent.communal2.R713020QuesSub.R713020QuesSubDao;
import com.cnsoft.persistent.communal2.R713021QuesSubItem.R713021QuesSubItemDBO;
import com.cnsoft.persistent.communal2.R713021QuesSubItem.R713021QuesSubItemDao;
import com.cnsoft.persistent.player2.U104060QuesVal.U104060QuesValDBO;
import com.cnsoft.persistent.player2.U104060QuesVal.U104060QuesValDao;

/**
 * 问卷信息业务处理
 */
@Component("WXPlayerQuesBusinesslogic")
//@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class WXPlayerQuesBusinesslogic extends MyBusinessSupport {
	@Resource
	private R713010QuesDao daoR713010Ques;
	@Resource
	private R713020QuesSubDao daoR713020QuesSub;
	@Resource
	private R713021QuesSubItemDao daoR713021QuesSubItem;
	@Resource
	private U104060QuesValDao daoU104060QuesVal;

	// 一览（分页查询）
	public RESTResultBean<R713010QuesDBO> doIndex(PageModel<R713010QuesDBO> pageModel) {
		// 定义返回结果集合
		RESTResultBean<R713010QuesDBO> result = new RESTResultBean<R713010QuesDBO>();
		// 分页查询
		result.setData(daoR713010Ques.doSelectPage(pageModel));
		// 设定提示信息
		result.setResult(ECCodeMessageConstants.MESSAGE_DB_SELECT);
		// 输出结果日志
		logger.debug("result=====>>>>" + result.toString());
		return result;
	}

	// 信息详情
	public Map<String, ?> loadData(R713010QuesDBO ques) {
		// 定义返回结果集合
		Map<String,Object> data = new HashMap<>();
		
		// 读取问卷基本信息
		ques = daoR713010Ques.doRead(ques);
		
		// 读取问卷题目信息
		PageModel<R713020QuesSubDBO> pageModel = new PageModel<>();
		R713020QuesSubDBO subParam = new R713020QuesSubDBO();
		subParam.setQuesId(ques.getId());
		pageModel.setFormParamBean(subParam);
		pageModel.setOrderby("sub_sort DESC");//设定排序
		List<R713020QuesSubDBO> subDatas = daoR713020QuesSub.doSelectData(subParam);
		
		// 读取问卷题目选项信息
		if(EmptyHelper.isNotEmpty(subDatas)) {
			R713021QuesSubItemDBO subItemParam = new R713021QuesSubItemDBO();
			for(R713020QuesSubDBO item : subDatas) {
				subItemParam.setQuesId(ques.getId());
				subItemParam.setQuesSubId(item.getId()); 
				
				item.setQuesSubItems(daoR713021QuesSubItem.doSelectData(subItemParam));
			}
		}

		// 数据返回
		data.put("ques", ques);
		data.put("subDatas", subDatas);
		
		// 查询详情
		logger.debug("data=====>>>>" + data);
		return data;
	}

	//检查是否已经填写
	public boolean checkData(R713010QuesDBO ques) {
		//检查是否已经填写
		U104060QuesValDBO quesValParam = new U104060QuesValDBO();
		quesValParam.setUserId(SessionHelper.currentUserId());
		quesValParam.setQuesId(ques.getId());
		List<U104060QuesValDBO> list = daoU104060QuesVal.doSelectData(quesValParam);
		
		return EmptyHelper.isNotEmpty(list);
	}
	
	public RESTResultBean<String> saveData(U104060QuesValDBO param) {
		// 定义返回结果集合
		RESTResultBean<String> result = new RESTResultBean<String>();
		
		param.setStartDay(DateHelper.currentTimeMillisCN3());
		param.setStartTime(DateHelper.currentTime2());
		
		param.setUserId(SessionHelper.currentUserId());
		
		// 信息插入
		daoU104060QuesVal.doInsertOrUpdate(param);
		// 设定提示信息
		result.setResult(ECCodeMessageConstants.MESSAGE_DB_INSERT);
		// 输出结果日志
		logger.debug("result=====>>>>" + result.toString());
		return result;
	}
}
